package no.gnet.edvd.cron;

import java.util.GregorianCalendar;

import no.gnet.edvd.DataStore;
import no.gnet.edvd.database.inmemory.InMemoryDatastore;
import no.gnet.edvd.engines.DefaultEngine;
import no.gnet.edvd.statemachine.BetState;
import no.gnet.edvd.types.Bet;
import no.gnet.edvd.types.Currency;
import no.gnet.edvd.types.Event;
import no.gnet.edvd.types.Player;
import no.gnet.edvd.types.StringOption;
import no.gnet.edvd.types.YesNoEvent;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;

public class StateJobTest {
	InMemoryDatastore datastore;
	Player EDDEVEDDE_PLAYER;
	private Player jimmy,danny,fanny;

	@Before
	public void setup() {
		EDDEVEDDE_PLAYER = new Player("EddeVedde", "EddeVedde", "mr.eddevedde@gmail.com");
		jimmy = new Player("jimmy", "jimmyson", "gaute.smaland@miles.no");
		danny = new Player("danny", "danny", "gsmaland@miles.no");
		fanny = new Player("fanny", "fanny", "gs@miles.no");

		datastore = new InMemoryDatastore();
	}	
	
	@Test
	public void test() throws Exception {
		
		
		Event sitterRegjeringen = getYesNoEvent("Sitter den nye regjeringen hele perioden?");
		
		Event storedevent = datastore.storeEvent(sitterRegjeringen);
		StringOption yesoption = StringOption.YES(sitterRegjeringen);
		Bet yesbet = new Bet(sitterRegjeringen, yesoption, 100.0, Currency.NOK, jimmy, new DateTime());
		yesbet.id = InMemoryDatastore.inVMautoInc();
		StringOption nooption = StringOption.NO(sitterRegjeringen);
		Bet nobet = new Bet(sitterRegjeringen, nooption, 200.0, Currency.NOK,danny,new DateTime());
		nobet.id = InMemoryDatastore.inVMautoInc();
		Bet anobet = new Bet(sitterRegjeringen, nooption, 400.0, Currency.NOK,fanny,new DateTime());
		anobet.id = InMemoryDatastore.inVMautoInc();
		Bet anotherbet = new Bet(sitterRegjeringen, nooption, 400.0, Currency.NOK, jimmy, new DateTime());
		anotherbet.id = InMemoryDatastore.inVMautoInc();		
		datastore.placeBets(yesbet,nobet,anobet,anotherbet);
		storedevent.state=BetState.EVENT_DECIDED;
		storedevent = datastore.storeEvent(sitterRegjeringen);
		datastore.setWinnerForEvent(storedevent.id, yesoption.id);
		
		run(datastore);
		
		
		
		
	}

	private void run(DataStore ds) throws Exception {
		StateJob sjob = new StateJob();
		sjob.setDatastore(ds);
		sjob.setEngine(new DefaultEngine());
		sjob.execute();
	}

	private void createTestEventWithState(BetState state) {
		Event event = getYesNoEvent("aabadabim");
		event.state=state;
		Event storedEvent = datastore.storeEvent(event);
		Event insertedE = datastore.getBettableEvent(storedEvent.id);
	}
	
	private Event getYesNoEvent(String q) {
		Event sitterRegjeringen = new YesNoEvent(q);
		sitterRegjeringen.description = q;
		sitterRegjeringen.id = InMemoryDatastore.inVMautoInc();
		
		GregorianCalendar calendar_dl = new GregorianCalendar();
		//Advance the calendar one day:
		calendar_dl.add(GregorianCalendar.MONTH, 1);		
		DateTime inAMonth = new DateTime(calendar_dl.getTime());
		sitterRegjeringen.betDeadline=inAMonth;
		
		sitterRegjeringen.eventDecidedTime = inAMonth;
		sitterRegjeringen.owner = EDDEVEDDE_PLAYER;
		return sitterRegjeringen;
	}	

}
